"use strict";
var _checkstat, _userID, _functionID;
var $container, $table, _idField;
var _checkstage, _canRollBack, _selNode, _inited;

function init(stage, canRollBack) {
	if(!stage) {
		return;
	}
	//	if(!_menuID) {
	//		_menuID = 1;
	//	}
	_checkstage = stage;
	_canRollBack = canRollBack;
	_selNode = null;
	_inited = false;

	if(!_checkstat) {
		_checkstat = 0;
	}

	getyear();
	getCheckStat("数据状态");

	_userID = vw_static.getCookie(window.location.host);
	_functionID = vw_static.getQueryString(window, "fid");
	$container = $('#structure');

	var html = [
		'<div class="pull-left" style="margin-right: 5px;background-color: #f7f7f9;border: 1px solid #e1e1e8; border-radius: 4px;padding: 3px 10px;">',
		'	<strong>审核状态：</strong>',
		'	<select id="select_checkStat" style="width:100px">',
		'		<option value="0">未审核</option>',
		'		<option value="1">已审核</option>',
		'	</select>',
		'</div>',
		'<button type="button" id="fn_verify" class="btn btn-primary" style="margin-top: 3px"><i class="fa fa-gavel"></i> 审核</button>',
		_canRollBack ? '<button type="button" id="fn_rollBack" class="btn btn-warning" style="margin-top: 3px"><i class="fa fa-reply"></i> 取消审核</button>' : '',
	].join('');

	$container.vw_structure('destroy').vw_structure({
		searchtree: {
			//			menu: [{
			//				id: 1,
			//				text: "按报表分类"
			//			}, {
			//				id: 2,
			//				text: "按调查对象分类"
			//			}],
			defaultMenuID: 2,
			//			onMenuChanged: function(menu) {
			//				_menuID = menu;
			//			},
			selectFirst: true,
			onTreeSelected: function(node) {
				_selNode = node;
				if(_inited) {
					fn_locate(node);
				}
			}
		},
		edittable: {
			editable: false,
			operate: operate,
			ExtraTools: html,
			columnFormatter: _columnFormatter,
			onInitialized: function() {
				$('#fn_verify').click(fn_verify);
				$('#fn_rollBack').click(fn_rollBack);

				$('#select_checkStat').select2()
					.val(_checkstat)
					.trigger('change')
					.on('select2:select', function(e) {
						_checkstat = e.params.data.id;
						$container.vw_structure('getOptions').edittable.extrapara.checkstat = _checkstat;
						$container.vw_structure('getEdittable').vw_edittable('getOptions').extrapara.checkstat = _checkstat;
						var node = $container.vw_structure('getSearchtree').vw_searchtree('GetSelected')[0];
						$container.vw_structure('getEdittable').vw_edittable('ajaxLoadTableData', node.id, node.text, 2);
					});
			},
			onAllEvents: function() {
				$('#fn_verify').prop('disabled', !(_checkstat == 0 && $container.vw_structure('getEdittable').vw_edittable('getSelections').length));
				$('#fn_rollBack').prop('disabled', !(_checkstat == 1 && $container.vw_structure('getEdittable').vw_edittable('getSelections').length));
			}
		},
		//		showMap: true,
		//		onTabShown: function(e) {
		//			_inited = (e == "map");
		//			fn_locate(_selNode);
		//		},
		tabMapName: "日志轨迹",
		interfaceAddress: VW_INTERFACE.WEBSERVICE.DataVerify,
		extrapara: {
			userID: _userID,
			functionID: _functionID,
			checkstat: _checkstat,
			checkstage: _checkstage,
			year: _year.currentYear
		},
	});

	$table = $container.vw_structure('getEdittable');
	_idField = $table.vw_edittable('getOptions').idField;
}

function fn_locate(node) {
	vw_common.customAjax({
		url: VW_INTERFACE.WEBSERVICE.ObjectManage('GetObject'),
		type: "GET",
		dataType: "json",
		data: {
			"catalogID": node.id
		}
	}, function(e) {
		$container.vw_structure('getMap').vw_map('refresh').vw_map("drawPolygon", e.data, true);

		if(!node.nodes) {
			vw_common.customAjax({
				url: VW_INTERFACE.WEBSERVICE.ObjectManage('GetNatureLand'),
				type: "GET",
				dataType: "json",
				data: {
					"code": node.id
				}
			}, function(e) {
				$container.vw_structure('getMap').vw_map("drawPolygon", e.data, false, {
					color: "#FFFACD",
					weight: 1
				});
			});

			vw_common.customAjax({
				url: VW_INTERFACE.WEBSERVICE.ObjectManage('GetDataLog'),
				type: "GET",
				dataType: "json",
				data: {
					"code": node.id
				}
			}, function(e) {
				console.log(e)
				e.data.head.id = 0;
				e.data.head.enums = {
					key: 5,
					dic: ["启动", "下载", "上传", "拍照", "保存"]
				};
				$container.vw_structure('getMap').vw_map("drawPoint", e.data, false, true, 8);
			});
		} else {
			$.each(node.nodes, function(k, val) {
				vw_common.customAjax({
					url: VW_INTERFACE.WEBSERVICE.ObjectManage('GetObject'),
					type: "GET",
					dataType: "json",
					data: {
						"catalogID": val.id
					}
				}, function(e) {
					$container.vw_structure('getMap').vw_map("drawPolygon", e.data, false, {
						color: "#FFFACD",
						weight: 2
					});
				});
			});
		}
	});
}

function fn_verify() {
	var dom = [
		'<div style="margin:10px;padding-left:10px;background-color: #f7f7f9;border: 1px solid #e1e1e8; border-radius: 4px;">',
		'	<strong>审核状态：</strong>',
		'	<label class="radio-inline">',
		'		<input type="radio" name="inlineRadioOptions" value="1"> 通过',
		'	</label>',
		'	<label class="radio-inline">',
		'		<input type="radio" name="inlineRadioOptions" value="0"> 不通过',
		'	</label>',
		'</div>',
		'<div style="margin:10px">',
		'	<textarea id="textarea_msg" style="width:100%;resize:none" rows="6" placeholder="请输入审核意见..."></textarea>',
		'</div>',
		'<div id="check_msg_container" style="margin:10px" class="hidden">',
		'	<strong>常用语：</strong>',
		'	<div id="check_msg" style="margin:10px 0 0 20px"></div>',
		'</div>'
	].join('');

	bDialog.open({
		title: "审核意见",
		dom: dom,
		dialogMaxButton: false,
		customClass: 'blue',
		width: 400,
		height: 400,
		fnButton: true,
		fnButtonName: "审核",
		onShow: function(dialog) {
			$(dialog.dialog[0]).find('.radio-inline').css({
				"font-size": "18px",
				"padding": "10px 40px",
			});

			$(dialog.dialog[0]).find("input[name='inlineRadioOptions']").css({
				"margin-top": "6px",
				"margin-left": "-18px"
			});

			$($(dialog.dialog[0]).find('.radio-inline')[0]).css({
				"color": "green"
			});

			$($(dialog.dialog[0]).find('.radio-inline')[1]).css({
				"color": "red"
			});

			vw_common.customAjax({
				url: VW_INTERFACE.WEBSERVICE.TableManage('GetTableData'),
				type: "GET",
				dataType: "json",
				data: {
					table: "TB_BIZ_CHECKMSG"
				}
			}, function(e) {
				var data = {
					pass: "",
					unpass: []
				};
				$.each(e.data, function(k, val) {
					if(val[1] == 1) {
						data.pass = val[2];
					} else if(val[1] == 0) {
						data.unpass.push(val[2]);
					}
				});
				$(dialog.dialog[0]).find("input[name='inlineRadioOptions']").on('click', function() {
					initMsg($(this).val(), data);
				})
			});

			function initMsg(type, e) {
				$('#check_msg').empty();
				if(type == 1) {
					$('#textarea_msg').text(e.pass);
					$('#check_msg_container').addClass('hidden');
				} else {
					$('#textarea_msg').text("");
					$('#check_msg_container').removeClass('hidden');
					$.each(e.unpass, function(k, val) {
						$('#check_msg').append(['<div class="checkbox">',
							'<label class="bg-info">',
							'<input type="checkbox" value="">' + val + '</label>',
							'</div>'
						].join(''));
					});
					$('#check_msg').find('p').css({
						'border-radius': '4px',
						'padding': '0 5px'
					});
					$('#check_msg').find('label').off('click').on('click', function() {
						var msg = [];
						$.each($('#check_msg').find('input'), function(k, val) {
							if(val.checked) {
								msg.push($(val).parent().text());
							}
						});
						$('#textarea_msg').text(msg.join('；'));
					})
				}
			}
		},
		fn: function() {
			var stat = $("input[name='inlineRadioOptions']:checked").val();
			var msg = $('#textarea_msg').val();

			if(stat == undefined) {
				bDialog.msgPopup("warning", "提示", "请选择审核结果!");
				return;
			}

			if(stat == 0 && msg == "") {
				bDialog.msgPopup("warning", "提示", "请输入审核意见!");
				return;
			}

			bDialog.close({
				result: {
					stat: stat,
					msg: msg
				}
			});
		},
		callback: function(data) {
			if(data.results != null) {
				var ids = $.map($table.vw_edittable('getSelections'), function(row) {
					return row[_idField];
				});

				vw_common.customAjax({
					url: VW_INTERFACE.WEBSERVICE.DataVerify("DataVerify"),
					type: "GET",
					dataType: "json",
					data: {
						userID: _userID,
						id: ids.join(','),
						checkstage: _checkstage,
						status: data.results[0].result.stat,
						opinion: data.results[0].result.msg
					}
				}, function(e) {
					init(_checkstage, _canRollBack);
					$.Notify({
						caption: "成功",
						content: "审核结果已提交",
						type: "success"
					});
				});
			}
		},
	}, {
		messageType: 'confirm',
		cancel: function() {}
	});
}

function fn_rollBack() {
	bDialog.alert('确认取消审核吗？取消后将回滚到本阶段未审核状态！', function() {
		var ids = $.map($table.vw_edittable('getSelections'), function(row) {
			return row[_idField];
		});

		vw_common.customAjax({
			url: VW_INTERFACE.WEBSERVICE.DataVerify('CancleVerify'),
			type: "GET",
			dataType: "json",
			data: {
				id: ids.join(','),
				checkstage: _checkstage
			}
		}, function(e) {
			init(_checkstage, _canRollBack);
			$.Notify({
				caption: "成功",
				content: "审核已取消",
				type: "success"
			});
		});
	}, {
		messageType: 'confirm',
		cancel: function() {}
	});
}